package com.flea.hclv.service.impl;

import com.flea.hclv.model.dao.HCustomerPayRecordMapper;
import com.flea.hclv.model.entity.HCustomerPayRecord;
import com.flea.hclv.model.entity.vo.HCustomerPayRecordVo;
import com.flea.hclv.service.HCustomerPayRecordService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.dao.OrgDeptMapper;
import com.flea.system.entity.OrgDept;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project
 * @description: 客户缴费记录
 * @author: shiliqiang
 * @create: 2018-11-17 15:56
 * @version: 1.0
 */
@Service
public class HCustomerPayRecordServiceImpl implements HCustomerPayRecordService {

    @Autowired
    private HCustomerPayRecordMapper hCustomerPayRecordMapper;

    @Autowired
    private OrgDeptMapper orgDeptMapper;


    @Override
    public ServerResponse<FleaPage<HCustomerPayRecordVo>> selectAll(int curPage, int pageSize, String queryName, String queryStartTime, String queryEndTime) {
        List<OrgDept> orgDepts=null;
        if (StringUtils.isNotBlank(queryName)){
            orgDepts = orgDeptMapper.findByLikeName(queryName);
            if (CollectionUtils.isEmpty(orgDepts)){
                return ServerResponse.createBySuccess();
            }
        }
        RowBounds rowBounds = new RowBounds();
        List<HCustomerPayRecord> list = hCustomerPayRecordMapper.selectAll(rowBounds,orgDepts,queryStartTime,queryEndTime);
        FleaPage<HCustomerPayRecord> temp = new FleaPage<>(list);
        List<HCustomerPayRecordVo> voList = this.assembleCustomerPayRecordVo(list);

        FleaPage<HCustomerPayRecordVo> voFleaPage = new FleaPage<>();
        voFleaPage.setCurPageData(voList);
        voFleaPage.setCurPage(curPage);
        voFleaPage.setAllDataCount((int) temp.getAllDataCount());
        voFleaPage.setPageSize(pageSize);
        return ServerResponse.createBySuccess(voFleaPage);
    }

    private List<HCustomerPayRecordVo> assembleCustomerPayRecordVo(List<HCustomerPayRecord> list) {
        List<HCustomerPayRecordVo> voList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(list)) {
            for (HCustomerPayRecord aManageList : list) {
                HCustomerPayRecordVo vo = new HCustomerPayRecordVo();
                vo.setDeptId(aManageList.getDeptId());
                OrgDept dept = orgDeptMapper.selectByPrimaryKey(aManageList.getDeptId());
                vo.setDeptCode(dept.getDeptCode());
                vo.setDeptName(dept.getDeptName());
                vo.setStatus(aManageList.getStatus());
                vo.setPersonPhone(aManageList.getPersonPhone());
                vo.setPersonName(aManageList.getPersonName());
                vo.setCustomerPayRecordId(aManageList.getId());
                vo.setCost(aManageList.getCost());
                vo.setStatus(aManageList.getStatus());
                vo.setEndTime(aManageList.getEndTime());
                vo.setStartTime(aManageList.getStartTime());
                voList.add(vo);
            }
            return voList;
        }
        return null;
    }
}
